﻿using System;
using System.Collections.ObjectModel;
using System.Linq;

using Finance.Entites;
using Finance.Service.Interfaces;

namespace Finance.Service.Implements
{
    /// <summary>
    /// Store the data with json format in the disk
    /// </summary>
    /// <typeparam name="TEntity">The graph of store object contains in ObserableCollection</typeparam>
    public class MockChargeDataStore : JsonDataStore<Finance.Entites.Charge>
    {
        private const int MOCKITEMCOUNT = 300;
        private const int MOCKDAYRENGE = 70;

        public override ObservableCollection<Charge> Items
        {
            get
            {
                if (base._Items == null || base._Items.Count == 0)
                {
                    _Items = GenerateMockChargeDataStore();
                    base.Commit();
                }
                return base._Items;
            }
        }

        public ObservableCollection<Charge> GenerateMockChargeDataStore()
        {
            ObservableCollection<Charge> _ChargeDataStore = new ObservableCollection<Charge>();

            MockAccountDataStore _AccountDataStore = new MockAccountDataStore();
            MockPayoutClassificationDataStore _ClassificationDataStore = new MockPayoutClassificationDataStore();
            MockProjectDataStore _ProjectDataStore = new MockProjectDataStore();
            MockTraderDataStore _TraderDataStore = new MockTraderDataStore();

            var R = new Random();

            _ChargeDataStore.Clear();
            _ChargeDataStore.Add(
                    new PayoutCharge()
                    {
                        Account = _AccountDataStore.Items.First(),
                        Amount = 0,
                        Classification = _ClassificationDataStore.Items.First(),
                        SellerName = _TraderDataStore.Items.First().Name,
                        Project = _ProjectDataStore.Items.First(),
                        Date = DateTime.Now,
                    });
            _ChargeDataStore.Add(
                    new IncomeCharge()
                    {
                        Account = _AccountDataStore.Items.First(),
                        Amount = 0,
                        Classification = _ClassificationDataStore.Items.First(),
                        Project = _ProjectDataStore.Items.First(),
                        Date = DateTime.Now,
                    });
            for (int i = 0; i < MOCKITEMCOUNT; i++)
            {
                if (R.Next(-30, 100) > 0)
                {
                    _ChargeDataStore.Add(
                    new PayoutCharge()
                    {
                        Account = _AccountDataStore.Items.GetRandomItem(),
                        Amount = R.Next(1, 100),
                        Classification = _ClassificationDataStore.Items.GetRandomItem(),
                        SellerName = _TraderDataStore.Items.GetRandomItem().Name,
                        Project = _ProjectDataStore.Items.GetRandomItem(),
                        Date = DateTime.Now.AddDays(-R.Next(0, MOCKDAYRENGE)),
                    });
                }
                else
                {
                    _ChargeDataStore.Add(
                    new IncomeCharge()
                    {
                        Account = _AccountDataStore.Items.GetRandomItem(),
                        Amount = R.Next(1, 1000),
                        Classification = _ClassificationDataStore.Items.GetRandomItem(),
                        Project = _ProjectDataStore.Items.GetRandomItem(),
                        Date = DateTime.Now.AddDays(-R.Next(0, MOCKDAYRENGE)),
                    });
                }
            }
            return _ChargeDataStore;
        }
    }
}
